package com.xcx.eduservice.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcx.commonuntils.MD5;
import com.xcx.commonuntils.returnuntls.ErrorEnum;
import com.xcx.commonuntils.returnuntls.ReturnMessage;
import com.xcx.commonuntils.returnuntls.ReturnMessageUtils;

import com.xcx.eduservice.entity.aclentity.Role;
import com.xcx.eduservice.entity.aclentity.User;
import com.xcx.eduservice.entity.aclentity.UserRole;
import com.xcx.eduservice.mapper.RoleMapper;
import com.xcx.eduservice.mapper.UserMapper;
import com.xcx.eduservice.mapper.UserRoleMapper;
import com.xcx.eduservice.request.aclrequest.userrequest.AddUseRequest;
import com.xcx.eduservice.request.aclrequest.userrequest.UserGetProcessionRequest;
import com.xcx.eduservice.request.aclrequest.userrequest.UserQueryList;
import com.xcx.eduservice.request.aclrequest.userrequest.UserUpdateRequest;
import com.xcx.eduservice.response.AssignResponse;
import com.xcx.eduservice.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-01-12
 */
@Service
@Primary
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    final private UserMapper userMapper;
    final private UserRoleMapper userRoleMapper;
    final  private RoleMapper roleMapper;


    @Override
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    public ReturnMessage<User> addUserByAdmin(AddUseRequest addUseRequest) {
        //判断用户是否存在
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("mobile",addUseRequest.getMobile());
        User userMoblie = userMapper.selectOne(userQueryWrapper);
        if (userMoblie != null){
            ReturnMessageUtils.errorWithEum(ErrorEnum.E_USER_EXIT);
        }
        //如果不存在则新增用户并且默认赋予一个普通会员角色
        User addUser = new User();
        BeanUtils.copyProperties(addUseRequest, addUser);
        addUser.setPassword(MD5.encrypt(addUseRequest.getPassword()));

        userMapper.insert(addUser);
        //同时关联角色（普通会员）
        UserRole userRole = new UserRole();
        userRole.setUserId(addUser.getId());
        userRole.setRoleId("1196300996034977794");
        userRoleMapper.insert(userRole);
        return ReturnMessageUtils.success();
        }

    @Override
    public ReturnMessage<User> updateUser(UserUpdateRequest respVo) {

        User user = userMapper.selectById(respVo.getId());
        if (user == null){
            ReturnMessageUtils.errorWithEum(ErrorEnum.E_USER_NOT_EXIST);
        }

        User updateUser = new User();
        BeanUtils.copyProperties(respVo, updateUser);
        userMapper.updateById(updateUser);

        return ReturnMessageUtils.success();
    }

    @Override
    public ReturnMessage<Object> quaryList(UserQueryList respVo) {

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.orderByDesc("id");
        Integer count = userMapper.selectCount(userQueryWrapper);

        IPage<User> objectPage = new Page<>(respVo.getPageNo(), respVo.getPageSize(), count);

        IPage<User> userIPage = userMapper.selectPage(objectPage, userQueryWrapper);


        return ReturnMessageUtils.success(userIPage);
    }

    @Override
    public ReturnMessage<AssignResponse> toAssign(UserGetProcessionRequest respVo) {
        String userId = respVo.getId();
        User user = userMapper.selectById(userId);
        List<Role> assignResponse = userMapper.selectRolebyUserId(userId);

        //List<String> collect = assignResponse.stream().map(Role::getRoleName).collect(Collectors.toList());

        AssignResponse assignResponse1 = new AssignResponse();
        BeanUtils.copyProperties(user, assignResponse1);


        for (Role role:assignResponse
             ) {

            assignResponse1.getRoleList().add(role);

        }


        //List<Role> rolebyUserId = roleMapper.getRolebyUserId(userId);
        System.out.println(assignResponse1);
        return ReturnMessageUtils.success(assignResponse1);
    }







}
